package cj.web.marriage.controllers;

import java.text.ParseException;
import java.util.EnumSet;
import java.util.List;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.DateUtils;
import cj.web.admin.constants.MemuEnum;
import cj.web.marriage.constants.MarriageBusinessStateEnum;
import cj.web.marriage.constants.MarriageInterestingEnum;
import cj.web.marriage.constants.MarriageBusinessTypeEnum;
import cj.web.marriage.dao.YYBookingDAO;
import cj.web.marriage.domain.YYBooking;

@Path("/yYBooking")
public class YYBookingController{

	private static Logger logger = Logger.getLogger(YYBookingController.class.getName());
	
	@Autowired
	private YYBookingDAO yYBookingDao;
	
	@Get("show")
	public String show(Model model,YYBooking yYBooking) {
		//MemuEnum
        model.add("MemuEnum",MemuEnum.yYBooking);
        model.add("BusinessTypeEnum",EnumSet.allOf(MarriageBusinessTypeEnum.class));
        model.add("BusinessStateEnum",EnumSet.allOf(MarriageBusinessStateEnum.class));
        return "/views/marriageYY/yYBookingList.vm";
	}

	@Post("getList")
	public String queryList(YYBooking yYBooking) throws ParseException {
		JSONObject jo = new JSONObject();
		List<YYBooking> objs = yYBookingDao.query(yYBooking);
		
		for(YYBooking obj:objs){
			obj.setBookingAt(DateUtils.formatDateStr(obj.getBookingAt(), DateUtils.DATE_FULL_STR));
			obj.setCreateAt(DateUtils.formatDateStr(obj.getCreateAt(), DateUtils.DATE_FULL_STR));
		}
		
		int size = yYBookingDao.querysize(yYBooking);
		jo.put("data", objs);
		jo.put("draw", yYBooking.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@"+jo.toString();
	}
	
	@Get("updateStateById/{id:[0-9]+}")
	public String updateStateById(Model model,@Param("id") int id){
		JSONObject jo = new JSONObject();
		
		YYBooking obj = yYBookingDao.queryById(id);
		if(obj!=null){
			yYBookingDao.updateStateById(id);
		}
		
		return "@" + jo.toString();
	}
	
	@Get("deleteById/{id:[0-9]+}")
	public String deleteById(Model model,@Param("id") int id){
		YYBooking obj = yYBookingDao.queryById(id);
		
		if(obj!=null){
			yYBookingDao.deleteById(id);
		}
		return "r:../show";
	}
}